package util;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PermissionUtil {
    // 检查权限，返回true表示已经完全启用权限，返回false表示未启用权限
    public static boolean checkPermission(Activity activity,String[] permissions,int requestcode){
        // 判断版本6.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            int check = PackageManager.PERMISSION_GRANTED;
            for (String permission: permissions){
                check = ContextCompat.checkSelfPermission(activity,permission);
                if (check != PackageManager.PERMISSION_GRANTED){
                    break; // 只要有一个权限没有开启，就退出
                }
            }
            // 美开启权限，请求系统弹窗，让用户选择是否开启权限
            if (check != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(activity,permissions,requestcode);
                return false;
            }
        }
        return true;
    }

    public static boolean checkGrant(int[] grantResults) {
        for ( int i =0 ; i < grantResults.length; i++){
            if (grantResults[i] == -1){
                return false;
            }
        }
        return true;
    }
}
